package com.service;

import java.util.HashMap;

import org.springframework.stereotype.Service;

import com.model.OrderDetail;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class OrderDetailService {
	static HashMap<String,String> orderDB = new HashMap<String,String> ();
	static 
	{
		orderDB.put("1","Peter");
		orderDB.put("2","Tom");
		orderDB.put("3","Mike");
	}
	
	@HystrixCommand(fallbackMethod = "getFallback")
	public OrderDetail getOrderDetailByID(String id) throws Exception
	{
		OrderDetail orderDetail = new OrderDetail();
		
		if("error".equals(id) )
		{
			throw new Exception();
		}
		
		orderDetail.setOrderId(id);
		orderDetail.setOrderOwner(orderDB.get(id));
		return orderDetail;
	}
	
	public OrderDetail getFallback(String orderId) {

		OrderDetail orderDetail = new OrderDetail();
		orderDetail.setOrderId("error");
		orderDetail.setOrderOwner("error");
		
		System.out.println("In fallbackForOrderDetail function");
		
		return orderDetail;
	}
}
